<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=641552
-->
<head>
  <title>Test for Bug 641552</title>
  <script type="application/javascript" src="/MochiKit/packed.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=641552">Mozilla Bug 641552</a>
<p id="display"></p>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 641552 **/

SimpleTest.waitForExplicitFinish();

var contractId = "@mozilla.org/xmlextras/xmlhttprequest;1";
var categoryEntries = [
  {category: "JavaScript-global-property", entry: "randomname", contractId: contractId},
];

function addCategoryEntries(func) {
  for (var categoryEntry of categoryEntries) {
    SpecialPowers.addCategoryEntry(categoryEntry.category, categoryEntry.entry, categoryEntry.contractId,
                                   false, true);
  }
  SimpleTest.executeSoon(func);
}

function removeCategoryEntries(func) {
  for (var categoryEntry of categoryEntries) {
    SpecialPowers.deleteCategoryEntry(categoryEntry.category, categoryEntry.entry, false);
  }
  SimpleTest.executeSoon(func);
}

function checkNamesPresent() {
  ok(window.randomname, "window.randomname should return an object");
}

function checkNamesAbsent() {
  ok(!window.randomname, "window.randomname should return undefined");
}

// Ensure the initial state
checkNamesAbsent();

addCategoryEntries(function test1() {
  ok(window.randomname, "window.randomname should return an object");

  delete window.randomname;

  // The delete opertor should have no effect as long as the category entry is registered.
  checkNamesPresent();

  removeCategoryEntries(test2);
});

function test2() {
  // The object should be cached on the global/navigator object once accessed.
  checkNamesPresent();

  delete window.randomname;

  // Now the delete opertor should have the effect.
  checkNamesAbsent();

  addCategoryEntries(function() {
    removeCategoryEntries(test3);
  });
}

function test3() {
  // The object should not be cached until the first access.
  checkNamesAbsent();

  SimpleTest.finish();
}

</script>
</pre>
</body>
</html>
